package com.pateo.retail.canal.handler.es;


import com.pateo.retail.canal.constant.CommonConstants;
import com.pateo.retail.canal.es.EsService;
import com.pateo.retail.canal.es.vo.EsAddReqVo;
import com.pateo.retail.canal.es.vo.EsUpsertReqVo;
import com.pateo.retail.canal.model.CustomerInfoFieldValueDetail;
import com.pateo.retail.canal.model.ChildCustomerJoinInfo;
import com.pateo.retail.canal.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@Component
@CanalTable(value = "customer_info_field_value_detail")
public class CustomerInfoFieldValueDetailHandler implements EntryHandler<CustomerInfoFieldValueDetail> {

    private Logger logger = LoggerFactory.getLogger(CustomerInfoFieldValueDetailHandler.class);

    @Autowired
    private EsService esService;

    @Override
    public void insert(CustomerInfoFieldValueDetail customerInfoFieldValueDetail) {
        logger.info("insert message  {}", customerInfoFieldValueDetail);

        if (StringUtils.isEmpty(customerInfoFieldValueDetail.getTenant_code())) {
            logger.info("数据异常tenantCode为null，不执行同步es同步逻辑-----{}", customerInfoFieldValueDetail);
            return;
        }

        EsUpsertReqVo reqVo = new EsUpsertReqVo();
        ///////////////////////////与父表关系建立///////////////////
        ChildCustomerJoinInfo childCustomerJoinInfo = new ChildCustomerJoinInfo();
        childCustomerJoinInfo.setName(CommonConstants.CUSTOMER_JOIN_INFO_CHILD);
        childCustomerJoinInfo.setParent(customerInfoFieldValueDetail.getCustomer_id() + "");
        customerInfoFieldValueDetail.setCustomer_join_info(childCustomerJoinInfo);
        reqVo.setParentFlag(false);
        reqVo.setRouting(customerInfoFieldValueDetail.getCustomer_id() + "");
        ///////////////////////////与父表关系建立///////////////////

        reqVo.setData(customerInfoFieldValueDetail);
        reqVo.setIndex(CommonUtils.getEsIndex(customerInfoFieldValueDetail.getTenant_code()));

        // 这里场景特殊子文档的es id有前缀需要组装一下，不能直接通过sid来作为es使用
        reqVo.setId(CommonUtils.getFieldId(customerInfoFieldValueDetail.getSid()));
        esService.upsert(reqVo);
    }

    @Override
    public void update(CustomerInfoFieldValueDetail before, CustomerInfoFieldValueDetail after) {
        logger.info("update before {} ", before);
        logger.info("update after {}", after);

        if (StringUtils.isEmpty(after.getTenant_code())) {
            logger.info("数据异常tenantCode为null，不执行同步es同步逻辑-----{}", after);
            return;
        }

        EsUpsertReqVo reqVo = new EsUpsertReqVo();

        ///////////////////////////与父表关系建立///////////////////
        ChildCustomerJoinInfo childCustomerJoinInfo = new ChildCustomerJoinInfo();
        childCustomerJoinInfo.setName(CommonConstants.CUSTOMER_JOIN_INFO_CHILD);
        childCustomerJoinInfo.setParent(after.getCustomer_id() + "");
        after.setCustomer_join_info(childCustomerJoinInfo);
        reqVo.setParentFlag(false);
        reqVo.setRouting(after.getCustomer_id() + "");

        reqVo.setData(after);
        reqVo.setIndex(CommonUtils.getEsIndex(after.getTenant_code()));
        reqVo.setId(CommonUtils.getFieldId(after.getSid()));
        esService.upsert(reqVo);
    }

    @Override
    public void delete(CustomerInfoFieldValueDetail customerInfoFieldValueDetail) {
        logger.info("delete  {}", customerInfoFieldValueDetail);

        if (StringUtils.isEmpty(customerInfoFieldValueDetail.getTenant_code())) {
            logger.info("数据异常tenantCode为null，不执行同步es同步逻辑-----{}", customerInfoFieldValueDetail);
            return;
        }

        // 这里场景特殊子文档的es id有前缀需要组装一下，不能直接通过sid来作为es _id来使用
        esService.delete(CommonUtils.getEsIndex(customerInfoFieldValueDetail.getTenant_code()), CommonUtils.getFieldId(customerInfoFieldValueDetail.getSid()));
    }
}
